package oneroom.admin.board;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.ByTimestampFileRenamePolicy;

import oneroom.admin.oneroom.roomModel;
import oneroom.comm.ThumbnailUtil;
import oneroom.comm.commPath;

public class boardService {
	/**
	 * 분기
	 * @param requests
	 * @return
	 */
	public boardModel setRequest(HttpServletRequest requests){
		boolean returnVal = true;
		MultipartRequest request = null;
		boardModel returnValue	=	new boardModel();
		boardDao 	dao 		= 	new boardDao();
		String gubun = requests.getParameter("gubun") == null ? "" :requests.getParameter("gubun") ;
		if(gubun != null && gubun.equals("")){ //등록 및 수정
			try {
				request = new MultipartRequest(requests, commPath.UPLOAD_PATH,
						commPath.SIZELIMIT, "UTF-8", new ByTimestampFileRenamePolicy("board"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			gubun = request.getParameter("gubun") == null ? "" :request.getParameter("gubun");
			if(gubun != null && gubun.equals("I")){
				 returnValue 	= setParame(request);
				 int max = dao.getBoardMax(returnValue.getBoard_kind());
				 returnValue.setData_id(max);
				 returnValue.setRe_data_id(max);
				 returnValue.setGubun(gubun);
				 returnValue.setReg_id((String)requests.getSession().getAttribute("id"));
				 returnValue.setReturnVal(dao.getInsert(returnValue));
			}else if(gubun != null && gubun.equals("U")){
				returnValue 	= setParame(request);
				returnValue.setGubun(gubun);
				returnValue.setReturnVal(dao.getUpdate(returnValue));
			}else if(gubun != null && gubun.equals("R")){
				 returnValue 	= setParame(request);
				 dao.getGroupUpdate(returnValue);
				 int max = dao.getBoardMax(returnValue.getBoard_kind());
				 //int deth = dao.getBoardDeth(request.getParameter("re_data_id"),request.getParameter("board_group"));
				 returnValue.setData_id(max);
				 returnValue.setBoard_group(Integer.parseInt((request.getParameter("board_group") == null ? "0" : request.getParameter("board_group")))+1);
				 returnValue.setDeth(Integer.parseInt((request.getParameter("deth") == null ? "0" : request.getParameter("deth")))+1);
				 returnValue.setGubun(gubun);
				 returnValue.setReg_id((String)requests.getSession().getAttribute("id"));
				 returnValue.setReturnVal(dao.getInsert(returnValue));
			}
		}else if(gubun.equals("D")){ //삭제
			String data_id = requests.getParameter("data_id") == null ? "0" : requests.getParameter("data_id");
			returnValue.setData_id(Integer.parseInt(data_id));
			fileDelete(requests.getParameter("att_file") 	== null ? "" : requests.getParameter("att_file"));
			returnValue.setGubun(gubun);
			returnValue.setBoard_kind(requests.getParameter("searchBoard" ));
			returnValue.setReturnVal(dao.getDelete(data_id));
		}
		return returnValue;
	}
	
	/**
	 * 파라미터 셋팅
	 * @param request
	 * @return
	 */
	private boardModel setParame(MultipartRequest request){
		boardModel returnValue 	= 	new boardModel();
		  returnValue.setData_id(Integer.parseInt((request.getParameter("data_id") == null ? "0" : request.getParameter("data_id"))));
		  returnValue.setTitle(request.getParameter("title" ) 		== null ? "" :request.getParameter("title"));
		  returnValue.setContent(request.getParameter("content" ) 	== null ? "" :request.getParameter("content"));
		  returnValue.setHit_count(Integer.parseInt((request.getParameter("hit_count" ) 	== null ? "0" :request.getParameter("hit_count"))));
		  returnValue.setReg_id(request.getParameter("reg_id" ) 	== null ? "" :request.getParameter("reg_id"));
		  returnValue.setReg_date(request.getParameter("reg_date" ) 	== null ? "" :request.getParameter("reg_date"));
		  returnValue.setDel_flag(request.getParameter("del_flag" ) == null ? "" :request.getParameter("del_flag"));
		  returnValue.setBoard_kind(request.getParameter("searchBoard" ) 	== null ? "" :request.getParameter("searchBoard"));
		  returnValue.setOrg_file( request.getOriginalFileName("file_name") == null ? "" : request.getOriginalFileName("file_name") );
		  returnValue.setAtt_file( request.getFilesystemName("file_name") == null ? "" : request.getFilesystemName("file_name") );
		  returnValue.setBoard_group(Integer.parseInt((request.getParameter("board_group") == null ? "0" : request.getParameter("board_group"))));
		  returnValue.setRe_data_id(Integer.parseInt((request.getParameter("re_data_id") == null ? "0" : request.getParameter("re_data_id"))));
		  returnValue.setDeth(Integer.parseInt((request.getParameter("deth") == null ? "0" : request.getParameter("deth"))));
		  
		  if(request.getParameter("gubun").equals("I")){
			  if(returnValue.getAtt_file() != null && returnValue.getAtt_file() !=""){
				  int pos = returnValue.getAtt_file().indexOf(".");
				  String ext = returnValue.getAtt_file().substring(pos+1);
				  
				  if(ext.toLowerCase().equals("jpg") || ext.toLowerCase().equals("gif") || ext.toLowerCase().equals("jpge")){
				  	createThumbnailFiles(returnValue.getAtt_file());
					String photoName  = returnValue.getAtt_file().substring(0, returnValue.getAtt_file().indexOf("."));
					String photoExtension  = returnValue.getAtt_file().substring(returnValue.getAtt_file().indexOf("."),returnValue.getAtt_file().length() );
					returnValue.setThumb_file(photoName+"_thumb141"+photoExtension);
				  }
			  }
		  }
		  if(request.getParameter("gubun").equals("U")){
			  if(request.getParameter("delfile") != null && request.getParameter("delfile").equals("Y")){//삭제를 선택 하였을 경우
				  returnValue.setOrg_file("");
				  returnValue.setAtt_file("");
				  //파일 삭제
				  fileDelete(request.getParameter("att_file") == null ? "" : request.getParameter("att_file"));
				  fileDelete(request.getParameter("thumb_file") == null ? "" : request.getParameter("thumb_file"));
			  }else{
				  if(returnValue.getOrg_file() != null && !returnValue.getOrg_file().equals("")){ //새로운 파일을 등록 하였을 경우
					  	//파일 삭제
					  	fileDelete(request.getParameter("att_file") == null ? "" : request.getParameter("att_file"));
					  	fileDelete(request.getParameter("thumb_file") == null ? "" : request.getParameter("thumb_file"));
				  }else{//액션이 없을 경우
					  	returnValue.setOrg_file(request.getParameter("org_file") == null ? "" : request.getParameter("org_file"));
					  	returnValue.setAtt_file(request.getParameter("att_file") == null ? "" : request.getParameter("att_file"));
					  if(returnValue.getOrg_file() != null && returnValue.getOrg_file() !=""){
						  int pos = returnValue.getAtt_file().indexOf(".");
						  String ext = returnValue.getAtt_file().substring(pos+1);
						  
						  if(ext.toLowerCase().equals("jpg") || ext.toLowerCase().equals("gif")|| ext.toLowerCase().equals("jpge")){
						  	createThumbnailFiles(returnValue.getAtt_file());
							String photoName  = returnValue.getAtt_file().substring(0, returnValue.getAtt_file().indexOf("."));
							String photoExtension  = returnValue.getAtt_file().substring(returnValue.getAtt_file().indexOf("."),returnValue.getAtt_file().length() );
							returnValue.setThumb_file(photoName+"_thumb141"+photoExtension);
						  }
					  }					  	
				  } 
			  }
		  }
		  return returnValue;	
	}

	/**
	 * 썸네일 이미지 생성
	 * @param attFile
	 */
	private void createThumbnailFiles(String attFile) {
		try {
			ThumbnailUtil.createThumbnail(commPath.UPLOAD_PATH +
									File.separator + attFile, commPath.THUMB_BOARD_SMALL_LENGTH_SIZE,commPath.THUMB_BOARD_SMALL_WIDTH_SIZE);
		} catch (Exception e) { }
	}
	/**
	 * 파일 삭제
	 * @param fileName
	 * @return
	 */
	public boolean fileDelete (String fileName){
		boolean returnValue = true;
		fileName = commPath.UPLOAD_PATH+fileName;
		File file = new File(fileName);
		if(file.isFile()){
			returnValue = file.delete();
		}
	
		return returnValue;
	}
}
